<?php
namespace app\admin\controller;

class Index extends Base{

    public function index(){

        return $this->fetch();
    }
    //系统设置
    public function sysconfig(){
        if(request()->isPost()){
            $data=input('post.');
            $where=[
                'id'=>1
            ];
            $datas=[
                'signed_count'=>$data['signed_count'],
                'notsign_count'=>$data['notsign_count'],
                'signed_money'=>$data['signed_money'],
                'notsign_money'=>$data['notsign_money'],
                'cur_money'=>$data['cur_money'],
                'cur_per'=>$data['cur_per'],
                'cut_money'=>$data['cut_money'],
                'profit_rate' => $data['profit_rate']
            ];
            $res=model('System')->where($where)->update($datas);
            if($res){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }
        $sys=model('System')->sysConfig();
        $this->assign('sys',$sys);
        return $this->fetch();
    }
    //用户管理
    public function userList(){
        $list=model('User')->getList();
        return $this->fetch('',[
            'list'=>$list
        ]);
    }
    //删除用户
    public function userdel($id){
        if(empty($id)){
            $this->error('参数错误','index/userlist');
        }
        $user=model('User');
        $res=$user->where(['id'=>$id])->delete();
        if($res){
            $this->success('删除成功','index/userlist','',1);
        }else{
            $this->error('删除失败','index/userlist','',1);
        }

    }

    //活动列表
    public function project(){
        $project=model('Project');
        $list=$project->order('id desc')->paginate(2);
        return $this->fetch('',[
            'list'=>$list
        ]);
    }

    //打卡列表
    public function itemlist(){
        $projectitem=model('Projectitem');
        $list=$projectitem->order('id desc')->paginate(2);
        return $this->fetch('',[
            'list'=>$list
        ]);
    }

    //优惠列表
    public function discounts_list(){
        $projectitem=model('Discounts');
        $list=$projectitem->order('id desc')->paginate(2);
        return $this->fetch('',[
            'list'=>$list
        ]);
    }

    //添加优惠券
    public function add_discount(){
        if(request()->isPost()){
            $data=input('post.');
            if(empty($data['money'])||empty($data['surplus'])||empty($data['expire'])){
                $this->error('内容不能为空','','',1);
            }
            $data['add_time']=time();
            $discount=model('Discounts')->create($data)->save();
            if($discount==0){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }
    //编辑优惠券
    public function edit_discount(){
        if(request()->isPost()){
            $data=input('post.');
            if(empty($data['money'])||empty($data['surplus'])||empty($data['expire'])){
                $this->error('内容不能为空','','',1);
            }
            $datas=[
                'money'=>$data['money'],
                'surplus'=>$data['surplus'],
                'expire'=>$data['expire'],
                'status'=>$data['status']
            ];
            $discount=model('Discounts')->where('id',$data['id'])->update($datas);
            if($discount){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }
        $id=input('id');
        $disInfo=model('Discounts')->where('id',$id)->find();
        $this->assign('disInfo',$disInfo);
        return $this->fetch();
    }

    //删除优惠券
    public function discountdel($id){
        if(empty($id)){
            $this->error('参数错误','index/discounts_list');
        }
        $user=model('Discounts');
        $res=$user->where(['id'=>$id])->delete();
        if($res){
            $this->success('删除成功','index/discounts_list','',1);
        }else{
            $this->error('删除失败','index/discounts_list','',1);
        }
    }


}